//
//  LogInViewController.m
//  SsehMaster
//
//  Created by 杨阳 on 2018/8/3.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "LogInViewController.h"
#import "RegistViewController.h"
#import "SsehViewController.h"
#import "AppDelegate.h"
#import "FindPassWordViewController.h"
#import "UserAgreementViewController.h"

@interface LogInViewController ()

@property (nonatomic,strong) UITextField * phoneTF, * passWordTF;

@property (nonatomic,strong) UIButton * logInButton;

@end

@implementation LogInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self baseSetting];
    [self layoutUI];
}

-(void)baseSetting{
    self.navigationItem.title = @"登录";
    NSString * AgreementStr = DEF_PERSISTENT_GET_OBJECT(Sseh_Agreement);
    if (kStringIsEmpty(AgreementStr) || ![AgreementStr isEqualToString:@"是"] ) {
        UserAgreementViewController * vc = [[UserAgreementViewController alloc]init];
        vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self presentViewController:vc animated:YES completion:nil];
    }
}

-(void)layoutUI{
    
    __weak typeof (self)weakSelf = self;
    
    UIImageView * bgImageView = [UIImageView new];
    //bgImageView.image = [UIImage imageNamed:@"login_Bg"];
    bgImageView.userInteractionEnabled = YES;
    bgImageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    UIImageView * logoImageView = [UIImageView new];
    logoImageView.image = [UIImage imageNamed:@"ssehLogo"];
    [bgImageView addSubview:logoImageView];
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bgImageView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(103, 103));
        make.top.equalTo(bgImageView.mas_top).offset((20+NAVIGATION_BAR_HEIGHT)*PROPORTION_HEIGHT_IPHONE6);
    }];    
    
    //手机号
    UIView * phoneView = [UIView new];
    phoneView.layer.borderColor = RGBColor(48, 48, 48).CGColor;
    phoneView.layer.borderWidth = 0.5f;
    phoneView.layer.cornerRadius = 5;
    [bgImageView addSubview:phoneView];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logoImageView.mas_bottom).offset(25*PROPORTION_HEIGHT_IPHONE6);
        make.left.equalTo(bgImageView).offset(38*PROPORTION_WIDTH_IPHONE6);
        make.right.equalTo(bgImageView).offset(-38*PROPORTION_WIDTH_IPHONE6);
        make.height.mas_equalTo(40);
    }];
    
    UIImageView * phoneImageView = [UIImageView new];
    phoneImageView.image = [UIImage imageNamed:@"account"];
    [phoneView addSubview:phoneImageView];
    [phoneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneView).offset(10*PROPORTION_WIDTH_IPHONE6);
        make.size.mas_equalTo(CGSizeMake(21, 23));
        make.centerY.equalTo(phoneView);
    }];
    
    self.phoneTF = [UITextField new];
    self.phoneTF.placeholder = @"请输入您的手机号码";
    self.phoneTF.keyboardType = UIKeyboardTypePhonePad;
    self.phoneTF.textColor = [UIColor blackColor];
    self.phoneTF.font = [UIFont systemFontOfSize:14];
    [self.phoneTF setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(phoneTFStaty) name:UITextFieldTextDidChangeNotification object:self.phoneTF];
    [phoneView addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneImageView.mas_right).offset(20*PROPORTION_WIDTH_IPHONE6);
        make.right.top.bottom.equalTo(phoneView);
    }];
    
    //密码
    
    UIView * pwView = [UIView new];
    pwView.layer.borderColor = RGBColor(48, 48, 48).CGColor;
    pwView.layer.borderWidth = 0.5f;
    pwView.layer.cornerRadius = 5;
    [bgImageView addSubview:pwView];
    [pwView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneView.mas_bottom).offset(10*PROPORTION_HEIGHT_IPHONE6);
        make.left.right.equalTo(phoneView);
        make.height.mas_equalTo(40);
    }];
    
    UIImageView * pwImageView = [UIImageView new];
    pwImageView.image = [UIImage imageNamed:@"passWord"];
    [pwView addSubview:pwImageView];
    [pwImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pwView).offset(10*PROPORTION_WIDTH_IPHONE6);
        make.size.mas_equalTo(CGSizeMake(21, 23));
        make.centerY.equalTo(pwView);
    }];
    
    self.passWordTF = [UITextField new];
    self.passWordTF.placeholder = @"请输入密码";
    //self.passWordTF.text = @"12345678";
    self.passWordTF.secureTextEntry = YES;
    self.passWordTF.textColor = [UIColor blackColor];
    self.passWordTF.font = [UIFont systemFontOfSize:14];
    [self.passWordTF setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passWordTFStaty) name:UITextFieldTextDidChangeNotification object:self.passWordTF];
    [pwView addSubview:self.passWordTF];
    [self.passWordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pwImageView.mas_right).offset(20*PROPORTION_WIDTH_IPHONE6);
        make.right.top.bottom.equalTo(pwView);
    }];
    
    //立即登录
    
    self.logInButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.logInButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.logInButton.layer.cornerRadius = 7;
    [self.logInButton setBackgroundColor:[UIColor appColor]];
    [self.logInButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.logInButton addTarget:self action:@selector(logInButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImageView addSubview:self.logInButton];
    [self.logInButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pwView.mas_bottom).offset(25*PROPORTION_HEIGHT_IPHONE6);
        make.left.right.equalTo(pwView);
        make.height.mas_equalTo(47);
    }];
    
    //找回密码  立即注册
    UIView * lineView = [UIView new];
    [bgImageView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgImageView);
        make.top.equalTo(weakSelf.logInButton.mas_bottom).offset(20*PROPORTION_HEIGHT_IPHONE6);
        make.size.mas_equalTo(CGSizeMake(1, 20));
    }];
    
    UIButton * findPWButton = [UIButton buttonWithType:UIButtonTypeCustom];
    findPWButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [findPWButton setTitle:@"找回密码" forState:UIControlStateNormal];
    [findPWButton setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
    [findPWButton addTarget:self action:@selector(findPWButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgImageView addSubview:findPWButton];
    [findPWButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(lineView.mas_centerY);
//        make.right.equalTo(lineView.mas_left).offset(-5);
        make.centerX.equalTo(bgImageView.mas_centerX);
        make.top.equalTo(weakSelf.logInButton.mas_bottom).offset(20*PROPORTION_HEIGHT_IPHONE6);
    }];
    
//    UIButton * registButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    registButton.titleLabel.font = [UIFont systemFontOfSize:14];
//    [registButton setTitle:@"立即注册" forState:UIControlStateNormal];
//    [registButton setTitleColor:[UIColor roseRed] forState:UIControlStateNormal];
//    [registButton addTarget:self action:@selector(registButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    [bgImageView addSubview:registButton];
//    [registButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(lineView.mas_centerY);
//        make.left.equalTo(lineView.mas_right).offset(5);
//    }];
}

#pragma mark - 登录 找回密码 注册
-(void)logInButtonClick:(UIButton *)button{
    
    if (self.phoneTF.text.length != 11) {
        [self showErrorTipWithTitle:@"手机号码输入错误"];
        return;
    }
    
    [self showLoading];
    
    NSDictionary * par = @{@"member_account":self.phoneTF.text,
                           @"member_password":self.passWordTF.text,
                           @"member_type":@"2",
                           @"jpush_id":kStringIsEmpty(DEF_PERSISTENT_GET_OBJECT(Jpush_TOKEN))?@"":DEF_PERSISTENT_GET_OBJECT(Jpush_TOKEN)
                           };
    
    [[QYNetManager sharedManager]POST:kLoginMember parameters:par andApiTyp:SsehApiTypMain SuccessBlock:^(id responseObject) {
        [self hideLoading];
        if ([responseObject[@"status"] isEqualToString:@"error"]) {
            [self showErrorTipWithTitle:responseObject[@"error"]];
        }else{
            //用户账号
            NSString * member_account = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"member_account"]];
            DEF_PERSISTENT_SET_OBJECT([NSString emptyStr:member_account], SsehUserAccount);
            
            //用户id
            NSString * member_id = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"member_id"]];
            DEF_PERSISTENT_SET_OBJECT([NSString emptyStr:member_id], SsehUserID);
            
            //token
            NSString * member_token = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"member_token"]];
            DEF_PERSISTENT_SET_OBJECT([NSString emptyStr:member_token], SsehUserToken);
            
            //用户真实姓名
            DEF_PERSISTENT_SET_OBJECT([NSString emptyStr:responseObject[@"data"][@"member_real_name"]], Sseh_member_real_name);
            
            //用户手机号
            DEF_PERSISTENT_SET_OBJECT([NSString emptyStr:responseObject[@"data"][@"member_phone"]], Sseh_member_phone);
            
            //订单量
            DEF_PERSISTENT_SET_OBJECT([NSString emptyStr:responseObject[@"data"][@"order_count"]], Sseh_order_count);
            
            //用户头像
            DEF_PERSISTENT_SET_OBJECT([NSString emptyStr:responseObject[@"data"][@"member_img"]], Sseh_member_img);
            
            [self loginApp];
        }
        
    } Failure:^(NSError *error) {
        [self hideLoading];
        [self showErrorTipWithTitle:SsehPoolNetStr];
    }];
    
}

-(void)loginApp{
    
    AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    SsehViewController * ssehtabBar = [[SsehViewController alloc]init];
    [appDelegate.window setRootViewController:ssehtabBar];
    
}

-(void)findPWButtonClick:(UIButton *)button{
    
    FindPassWordViewController * vc = [[FindPassWordViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)registButtonClick:(UIButton *)button{
    
    RegistViewController * vc = [[RegistViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
